# 掩模
# @Author ZhangGJ
# @Date 2021/11/26 15:03

import cv2.cv2 as cv2
import numpy as np


def mask1():
    mask = np.zeros((150, 150, 3), np.uint8)
    mask[50:100, 20:80, :] = 255
    cv2.imshow('mask1', mask)
    mask[:, :, :] = 255
    mask[50:100, 20:80, :] = 0
    cv2.imshow('mask2', mask)
    cv2.waitKey()
    cv2.destroyAllWindows()


def add():
    img = cv2.imread('../images/beach.jpg')
    sum1 = img + img
    sum2 = cv2.add(img, img)
    cv2.imshow('img', img)
    cv2.imshow('sum1', sum1)
    cv2.imshow('sum2', sum2)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 模拟三色叠加得白光
def mask2():
    img1 = np.zeros((150, 150, 3), np.uint8)
    img1[:, :, 0] = 255
    img2 = np.zeros((150, 150, 3), np.uint8)
    img2[:, :, 1] = 255
    img3 = np.zeros((150, 150, 3), np.uint8)
    img3[:, :, 2] = 255
    cv2.imshow('1', img1)
    cv2.imshow('2', img2)
    cv2.imshow('3', img3)
    img = cv2.add(img1, img2)
    cv2.imshow('1+2', img)
    img = cv2.add(img, img3)
    cv2.imshow('1+2+3', img)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 利用掩模遮盖想加结果
def mask3():
    img1 = np.zeros((150, 150, 3), np.uint8)
    img1[:, :, 0] = 255
    img2 = np.zeros((150, 150, 3), np.uint8)
    img2[:, :, 2] = 255

    img = cv2.add(img1, img2)
    cv2.imshow('no mask', img)

    m = np.zeros((150, 150, 1), np.uint8)
    m[50:100, 50:100, :] = 255
    cv2.imshow('mask', m)

    img = cv2.add(img1, img2, mask=m)
    cv2.imshow('use mask', img)

    cv2.waitKey()
    cv2.destroyAllWindows()


if __name__ == '__main__':
    mask3()
